from django.views import View
from django.db.models import Q
from django.shortcuts import render, HttpResponse, redirect, reverse


class ShowList(View):

    def search(self, field_list):
        query = self.request.GET.get('query', '')

        q = Q()
        q.connector = 'OR'
        for field in field_list:
            q.children.append(Q(('{}__contains'.format(field), query)))

        return q

    def post(self, request, *args, **kwargs):
        # 找操作的名字
        action = request.POST.get('action')
        # 反射获取方法
        if not hasattr(self, action):
            return HttpResponse('非法操作')

        ret = getattr(self, action)()  # 获取到对应的方法并执行

        if ret:
            return ret
            # 返回值
        return self.get(request, *args, **kwargs)
